<?php
if(isset($_POST['userID'])){
    $db = new Database();			
    //$points = /*$_POST['Points']*/$points;
    $type = stripcslashes($_POST["Name"]); 
    $id = $_POST['userID'];
    $header = $_POST['header'];
    $getPoints = $db->query("SELECT * FROM tbdealer WHERE dealerCode='$id' and expiryDate >= CURDATE()","array");
    foreach($getPoints as $data)
    {
	$dealerPoints = $data['pointsEarned'];
	foreach (extractForms($dealerPoints) as $key => $value) {
	    if(isset($total[$key]))
		$total[$key] += $value; 
	    else
		$total[$key] = $value;
	}
    }
    foreach ($total as $k => $v)  {
	
	$split = explode(" ", $k);
	//$header = $split[0]; // Header
	
	if(strpos($k, 'PLINE') !== false){
	    $remove_zero = str_replace("0","", $split[1]);
            $k = $remove_zero; // Sub Header
	}else{
	    $k = $k; // Sub Header
	}
	
	if($type==$k){
	    $points = redeemPoints($k,$v,$id);
	}
    }
    //$getProduct = $db->query("SELECT * FROM tbproduct WHERE points <= '$points' AND type='$type' AND date > '$dateExpired' AND header='$header' ","array");
    $getProduct = $db->query("SELECT * FROM tbproduct WHERE points <= '$points' AND type='$type' AND header='$header' ","array");
    $a=0;
    foreach($getProduct as $b){
	$a++;
    }
?>
	<div class="body_content">
            <div class="position">
		<div class="label_header_position">
		    <label class="h3">
			<b>		
			    <div style="float: left;margin-right: 10px;margin-top: 15px;">
			    <?php 
			    $dealer = $db->query("SELECT * FROM tbdealer WHERE dealerCode='$id'");
			    foreach($dealer as $info)
			    {
                                $dateExpired = $info['expiryDate'];
			    }
				echo $info['firstName']. " " . $info['lastName'];
			    
			    ?>    
			    </div>
			    <p style="float: left;margin-right: 10px;margin-top: 15px;">
			    -|-
			    </p>
			    <div style="float: left;margin-right: 10px;margin-top: 15px;">
			    <?php
                            //$pointsType = $_POST["Name"];
			    $getProductLines = $db->query("SELECT * FROM tbproduct_line WHERE code='$type' AND is_active='1'","array");
                            foreach($getProductLines as $productLine){
                                $code = $productLine['code'];
                                $description = $productLine['description'];
                            }
                            if($code==$type){
                                echo $description;
                            }else{
				echo $type;
			    }
                            ?>
			    </div>
			    <p style="float: left;margin-right: 10px;margin-top: 15px;">
			    -|-
			    </p>
			    <div style="float: left;margin-right: 10px;margin-top: 15px;">
			    <?php echo "<span id='dealerSubPoints'>"./*$_POST['Points']*/$points."<span>". " " . "Points"; ?>
			    </div>
			    <p style="float: left;margin-right: 10px;margin-top: 15px;">
			    -|-
			    </p>
			    <div style="float: left;margin-left: 10px;margin-top: 15px;">
			    <?php echo date('M d, Y'); ?>
			    </div>
                            
			</b>
                            <div style="float: right;margin-left: 10px;">
                                <input type="button" class="button BactToTop" value="Back to Dealer Points" />
                            </div>
		    </label>
		</div>
		<div class="right align_right">
                    
		</div>
		<div class="hr"></div>
		<?php
		if($a>0){
		?>
		<div class="content">
		    <div class="left">
			<input type="hidden" rel="<?php echo /*$_POST['Points']*/$points; ?>" originalPoints="<?php echo /*$_POST['Points']*/$points; ?>"  class="dealerpoints" value="<?php echo /*$_POST['Points']*/$points; ?>">
			<table border="0" id="table_accomodation" class="table_accomodation">
			    <thead>
				<tr>
				    <th>Product</th>
				    <th>Points need</th>
				    <th>Quantity</th>
				    <th>Actions</th>
				</tr>
			    </thead>
			    <tbody>
				<div id="load_redeem"></div>
				<?php
				$db = new Database();
				
				$points = /*$_POST['Points']*/$points;
				//$type = $_POST['Name'];
				//$getProduct = $db->query("SELECT * FROM tbproduct WHERE points <= '$points' AND type='$type' AND date > '$dateExpired' AND header='$header'  ");
				$getProduct = $db->query("SELECT * FROM tbproduct WHERE points <= '$points' AND type='$type'  AND header='$header'  ");
				
				foreach($getProduct as $data)
				{
				?>
				<tr>
				    <td><?php echo "&nbsp;".$data['product_name'];?></td>
				    <td><?php echo "&nbsp;".$data['points'];?></td>
				    <td class="actions"><input type="text" maxlength="3" onkeypress="return allownum()" style="width: 30px" id="quant_<?php echo $data['id'];?>"></td>
				    <td class="actions">
					<div style="margin-top: 2px;">
					    <center>
						<input type="hidden" id="info_<?php echo $data['id'];?>" class="<?php echo $data['type'] ?>" dealer_id='<?php echo $id ?>' name="" rel="<?php echo $data['points'];?>" />
						<img src="/images/icon/cart.png"  id="productredeem_<?php echo $data['id'];?>" class="cursor Productredeem" value="Add To Cart" rel="<?php echo $data['id'];?>" title="<?php echo $data['product_name']; ?>"/>
						<img src="/images/icon/edit.png"  id="editRedeem_<?php echo $data['id'];?>" rel="<?php echo $data['id'];?>" style="display: none" class="cursor editProduct" value="Edit" rel="<?php echo $data['id'];?>" title="Edit"/>
					    </center>
					</div>
				    </td>
				</tr>
				<?php
				}
				?>
			    </tbody>
			</table>
		    </div>
		    <div class="right">
			<div style="float: right; width: 90%; padding: 5px; border: 1px solid silver; min-height: 300px;" class="">
			    <div style="float: left; width: 100%; line-height: 23px; border-bottom: 1px solid silver;" class="h3">
				Product List 
			    </div>
			    <div style="float: left; width: 100%; line-height: 20px;" class="h4">
				<!--ol class="productList"></ol-->
                                <div style="float: left; min-height: 300px; width: 100%;">
                                    <table class="table_accomodation">
                                        <thead>
                                            <tr>
                                                <th>Product Name</th>
                                                <th>Quantity</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody class="productList">
                                            
                                        </tbody>
                                    </table>
                                </div>
                                <div style="float: left; min-height: 50px; width: 100%;">
                                    <div style="float: left;margin-top: 10px; width: 50%;">
                                        <label class="h3">
                                            Total:
                                        </label>
                                    </div>
                                    <div style="float: left;margin-top: 10px; width: 50%; text-align: right;">
                                        <label class="h3 totalSelectedPoints">
                                            0 Points
                                        </label>
                                    </div>
                                </div>
			    </div>
			</div>
			<div class="align_right fl_right" style="float: right; width: 90%;margin-top: 10px; line-height: 20px;">
			    <input type="button" value="Check Out" class="button checkOut" style="float: left;" prodType='<?php echo $type;?>' rel="<?php echo $id; ?>" />
			    <input type="button" value="Add Transaction" class="button addTransaction" prodType='<?php echo $type;?>' rel="<?php echo $id; ?>" />
			</div>
		    </div>
		</div>
		<?php
		}else{
		    echo "<h3>No Available Product</h3>";
		}
		?>
            </div>
        </div>
<?php
}
?>